/**  
 * @Title: GameController.java
 * @Package com.limit.control.admin
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-3 上午10:30:28
 * @version V1.0
 */ 
package com.limit.control.admin;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;
import com.limit.control.admin.validate.GameValidator;
import com.limit.exception.HHFileSystemException;
import com.limit.model.Admin;
import com.limit.model.Game;
import com.limit.model.GameType;
import com.limit.model.RankGame;
import com.limit.utils.HHFile;
import com.limit.utils.PropertiesUtil;
import com.limit.utils.json.JSONObject;

/** 
 * <br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-3 上午10:30:28 
 * @version V1.0
 */

public class GameController extends BaseController {

	public void index(){
		int p = this.getParaToInt("p",1);
		long admin_id = this.getParaToLong("admin_id", 0L);
		long gametype_id = this.getParaToLong("gametype_id", 0L);
		String exceptSelect = new String();
		if(admin_id == 0 && gametype_id == 0){
			exceptSelect = "from game order by sort_id desc";
			this.setAttr("games", Game.dao.paginate(p, pageSize, "select *", exceptSelect));
		}else if(admin_id != 0 && gametype_id != 0){
			exceptSelect = "from game where admin_id=? and type_id=? order by sort_id desc";
			this.setAttr("games", Game.dao.paginate(p, pageSize, "select *", exceptSelect , admin_id , gametype_id));
		}else if(admin_id != 0){
			exceptSelect = "from game where admin_id=? order by sort_id desc";
			this.setAttr("games", Game.dao.paginate(p, pageSize, "select *", exceptSelect , admin_id));
		}else if(gametype_id != 0){
			exceptSelect = "from game where type_id=? order by sort_id desc";
			this.setAttr("games", Game.dao.paginate(p, pageSize, "select *", exceptSelect , gametype_id));
		}
		this.setAttr("admins", Admin.dao.getListExceptAdmin());
		this.setAttr("gametypes", GameType.dao.getList());
		this.setAttr("admin_id", admin_id);
		this.setAttr("gametype_id", gametype_id);
		render("games.html");
	}
	
	public void view(){
		this._commonSetId();
		Game game = new Game();
		if(id > 0){
			game = Game.dao.findById(id);
		}
		this.setAttr("admins", Admin.dao.getListExceptAdmin());
		this.setAttr("gametypes", GameType.dao.getList());
		this.setAttr("game", game);
		render("game_view.html");
	}
	
	@Before(GameValidator.class)
	public void save(){
		List<UploadFile> uploadFiles = this.getFiles();
		Game game = this.getModel(Game.class);
		game.set("create_time", new Date());
		if(null != uploadFiles && !uploadFiles.isEmpty()){
			String newFileName = "" , path = "";
			for (UploadFile uploadFile : uploadFiles) {
				System.out.println(uploadFile.getParameterName());
				newFileName = System.currentTimeMillis()+HHFile.getExtension(uploadFile.getOriginalFileName());
				path = this._uploadFile(uploadFile, newFileName,PropertiesUtil.load("game_image_path"));
				if(uploadFile.getParameterName().equals("icon_path")){
					game.set("icon_path", path+newFileName);
				}else{
					game.set("image_path", path+newFileName);
				}
			}
		} 
		game.save();
		this.index();
	}
	
	
	@Before(GameValidator.class)
	public void update(){
		List<UploadFile> uploadFiles = this.getFiles();
		String old_icon_path 	= this.getPara("old_icon_path", "");
		String old_image_path 	= this.getPara("old_image_path", "");
		Game game = this.getModel(Game.class);
		if(null != uploadFiles && !uploadFiles.isEmpty()){
			for (UploadFile uploadFile : uploadFiles) {
				System.out.println(uploadFile.getParameterName());
				if(uploadFile.getParameterName().equals("icon_path")){
					if(!uploadFile.getFile().getPath().equals(old_icon_path)){
						String newFileName = System.currentTimeMillis()+HHFile.getExtension(uploadFile.getOriginalFileName());
						try {
							HHFile.deleteFile(old_icon_path);
						} catch (HHFileSystemException e) {
							System.out.println(e.getMessage());
						}
						String path = this._uploadFile(uploadFile, newFileName,PropertiesUtil.load("game_image_path"));
						
						game.set("icon_path", path+newFileName);
					}else{
						game.set("icon_path", old_icon_path);
					}
				}else if(uploadFile.getParameterName().equals("image_path")){
					if(!uploadFile.getFile().getPath().equals(old_image_path)){
						String newFileName = System.currentTimeMillis()+HHFile.getExtension(uploadFile.getOriginalFileName());
						try {
							HHFile.deleteFile(old_image_path);
						} catch (HHFileSystemException e) {
							System.out.println(e.getMessage());
						}
						String path = this._uploadFile(uploadFile, newFileName,PropertiesUtil.load("game_image_path"));
						
						game.set("image_path", path+newFileName);
					}else{
						game.set("image_path", old_image_path);
					}
				}
			}
		}else{
			game.set("icon_path", old_icon_path);
			game.set("image_path", old_image_path);
		}
		game.update();
		this.index();
	}
	
	
	public void delete(){
		this._commonSetId();
		Game game = Game.dao.findById(id);
		boolean temp = Game.dao.deleteById(id);
		if(temp){
			try {
				HHFile.deleteFile(game.getStr("icon_path"));
				HHFile.deleteFile(game.getStr("image_path"));
				HHFile.deleteFile(game.getStr("android_path"));
				HHFile.deleteFile(game.getStr("ios_path"));
			} catch (HHFileSystemException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	
	/**
	 * Ajax请求
	 * 关联到排行榜
	 */
	public void relateRank(){
		// 排序号rank_game->sort_id
		// 游戏IDrank_game->game_id
		// 排行IDrank_game->rank_id
		long game_id 	= this.getParaToLong("game_id", 0L);
		long rank_id 	= this.getParaToLong("rank_id", 0L);
		int sort_id		= this.getParaToInt("sort_id", -1);
		JSONObject json = new JSONObject();
		if(game_id <=0 || rank_id <=0){
			json.put("result", false);
			json.put("error", "参数有误");
			this.renderJson(json.toString());
			return ;
		}
		boolean isExist = RankGame.dao.findPrimaryRecord(rank_id, game_id);
		if(isExist){
			json.put("result", false);
			json.put("error", "记录已存在,无需再次点选");
			this.renderJson(json.toString());
			return ;
		}
		boolean result = RankGame.dao.set("game_id", game_id)
									 .set("rank_id", rank_id)
									 .set("sort_id", sort_id)
									 .save();
		if(result) {
			this.renderJson("result",true);
		}else{ 
			json.put("result", false);
			json.put("error", "保存失败");
			this.renderJson(json.toString());
		}
	}
	
}
